<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.issueTitle" placeholder="帖子标题" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.sectionId" placeholder="所属联盟" clearable filterable style="width: 150px;" class="filter-item">
        <el-option v-for="item in leagueList" :key="item.leagueId" :label="item.leagueName" :value="item.leagueId" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="100px" prop="issueId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.issueId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="帖子标题" align="center" max-width="500px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.issueTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属联盟" align="center" width="130px" prop="sectionId" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" type="primary"> {{ row.sectionName }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发帖人ID" align="center" width="110px" prop="founderId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.founderId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" width="150px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回帖数量" align="center" width="110px" prop="postNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.postNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点赞量" align="center" width="110px" prop="likeNum" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.likeNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" width="110px" prop="isPass" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.isPass===0" size="mini" type="primary">未审核</el-tag>
          <el-tag v-if="row.isPass===1" size="mini" type="success">通过</el-tag>
          <el-tag v-if="row.isPass===2" size="mini" type="danger">不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" width="160px" align="center" prop="createTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近回帖时间" width="160px" align="center" prop="updateTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row)">
            详情
          </el-button>
          <el-button type="primary" size="mini" @click="handlePost(row.issueId)">
            回帖
          </el-button>
          <el-button type="success" size="mini" @click="handlePass(row)">
            审核
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.issueId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showLeagueIssueList" />

    <el-dialog title="帖子详情" :visible.sync="dialogDetailVisible" width="600px">
      <el-card>
        <el-row>
          <el-col :span="24" class="issue-title">
            {{ tempDetail.issueTitle }}
          </el-col>
        </el-row>
        <el-row>
          <span class="middle-align"><el-image :key="tempDetail.headImg" :src="tempDetail.headImg" class="user-avatar" /></span>
          <span v-if="tempDetail.nickName" class="nickname middle-align">{{ tempDetail.nickName }}</span>
          <span v-else class="nickname middle-align">[暂未设置昵称]</span>
        </el-row>
        <el-row>
          <div v-if="tempDetail.issueContent" class="issue-content" v-html="tempDetail.issueContent"></div>
          <div v-else class="issue-content">[无内容]</div>
        </el-row>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核帖子" :visible.sync="dialogPassVisible" width="500px">
      <el-form ref="passForm" :rules="passRules" :model="tempPass" label-width="130px">
        <el-form-item label="审核结果" prop="isPass">
          <el-radio v-model="tempPass.isPass" :label="1">通过</el-radio>
          <el-radio v-model="tempPass.isPass" :label="2">不通过</el-radio>
        </el-form-item>
        <p style="margin-left: 50px">注：若审核不通过，该帖子将被隐藏！</p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPassVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="doPass">
          确认审核
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { showLeagueIssueList, getLeagueSectionList, getIssueDetail, deleteIssue, passIssue } from '@/api/discuss'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { isInteger } from '@/utils/validate'

export default {
  name: 'LeagueIssue',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        issueTitle: '',
        sectionId: undefined
      },
      dialogVisible: false,
      dialogDetailVisible: false,
      dialogPassVisible: false,
      tempDetail: {},
      tempPass: {
        issueId: undefined,
        isPass: undefined
      },
      passRules: {
        isPass: [{ required: true, message: '请选择审核结果', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }]
      },
      leagueList: []
    }
  },
  computed: {
    leagueMap: function() {
      return this.leagueList.reduce((acc, cur) => {
        acc[cur.leagueId] = cur.leagueName
        return acc
      }, {})
    }
  },
  created() {
    this.showLeagueIssueList()
    this.getLeagueSectionList()
  },
  methods: {
    showLeagueIssueList() {
      this.listLoading = true
      showLeagueIssueList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getLeagueSectionList() {
      getLeagueSectionList().then(response => {
        this.leagueList = response.data
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showLeagueIssueList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDialog() {
      this.tempHandle = {
        applyId: undefined,
        progress: undefined,
        handleNote: ''
      }
    },
    handleDetail(row) {
      getIssueDetail(row.issueId).then(response => {
        this.tempDetail = Object.assign({}, row)
        this.tempDetail.issueContent = response.data.issueContent
      })
      this.dialogDetailVisible = true
    },
    handlePass(row) {
      this.tempPass = {
        issueId: row.issueId,
        isPass: row.isPass
      }
      this.dialogPassVisible = true
    },
    doPass() {
      this.$refs['passForm'].validate((valid) => {
        if (valid) {
          passIssue(this.tempPass).then(response => {
            this.showLeagueIssueList()
            this.dialogPassVisible = false
            this.$notify({
              title: 'Success',
              message: '审核帖子成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handlePost(issueId) {
      this.$router.push({
        name: 'Post',
        params: {
          id: issueId,
          type: 2
        }
      })
    },
    handleDelete(issueId) {
      this.$confirm('此删除操作不可逆，该帖子下的所有回帖将无法查看，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteIssue(issueId).then(() => {
          this.showLeagueIssueList()
          this.$notify({
            title: 'Success',
            message: '删除帖子成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>

<style>
  .col-title {
    text-align: right
  }

  .col-title p {
    font-weight: bolder;
  }

  .col-content p {
    color: #848383
  }
  .user-avatar {
    cursor: pointer;
    width: 24px;
    height: 24px;
    border-radius: 12px;
  }
  .issue-title {
    font-weight: bold;
    font-size: medium;
    margin-bottom: 15px;
  }
  .nickname {
    color: #475669;
    margin-left: 10px;
  }
  .middle-align {
    vertical-align: middle;
  }
  .issue-content {
    margin-top: 20px;
  }
</style>
